package com.creditrank.indexer;
import java.sql.*;
import java.io.*;
import java.net.*;

/*
 * Author : Krishna Nagarjun Reddy
 */

public class Db_Retrieve
{
	
	public static IndexCreate ic;
	
	public static void connect()
	{
		
		String dbtime="";
		Blob dbdata;
		
	//	String dbUrl = "jdbc:mysql://mysql.cis.ksu.edu:3306/nagarjun";
		String dbUrl = "jdbc:mysql://mysql.cis.ksu.edu:3306/sudhikrr";
		//String dbClass = "com.mysql.jdbc.Driver";
		//String query = "Select * FROM test1";
		StringBuffer buff= new StringBuffer();
		int i=1;
		try 
		{

			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection (dbUrl,"sudhikrr","40Vlf0bK");
		//	Connection con = DriverManager.getConnection (dbUrl,"nagarjun","pikachu");
			//Statement stmt = con.createStatement();
			/*
			String url = "http://www.lib.k-state.edu";
        	URL pageURL = new URL(url);
            HttpURLConnection urlConnection = (HttpURLConnection) pageURL.openConnection();
           	urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
                       
           	InputStream in = new BufferedInputStream(urlConnection.getInputStream());
          	BufferedReader br1 = new BufferedReader(new InputStreamReader(in));
         
			int byteRead;
		    while ((byteRead = br1.read()) != -1)
                buff.append((char) byteRead);
		    
	    	String Query="INSERT into test1(URL,Content) VALUES (?,?)";
			
			ByteArrayInputStream io = new ByteArrayInputStream(buff.toString().getBytes("UTF-8"));
			
			PreparedStatement prestmt = con.prepareStatement(Query);
			prestmt.setString(1,url);
			prestmt.setBinaryStream(2, (ByteArrayInputStream)io,(int)buff.length());
			prestmt.executeUpdate();
			System.out.println("Hello..");*/
			String sb1="";
			for(int j=0;j<=1000;j++)

			{
				String sql = "SELECT url,content FROM example where id="+j;
				//String sql = "SELECT id,text FROM test2 where id="+j;
				
				PreparedStatement prestmnt = con.prepareStatement(sql);
				ResultSet rs = prestmnt.executeQuery();
				while (rs.next()) 
				{
					dbtime = rs.getString(1);
					dbdata=rs.getBlob(2);
					//dbdata="Hello";
					//System.out.print(dbtime);
					//System.out.println("     "+dbdata.toString()); 
					try
					{
						InputStream x=dbdata.getBinaryStream();
						int size=x.available();
						OutputStream out=new FileOutputStream("1.html");
						byte b[]= new byte[size];
					
						x.read(b);
						sb1=new String(b);
						//System.out.println(sb1);
						if(!sb1.equals("deadlink"))
						out.write(b);
						out.close();
						
					
					}
					catch (IOException ie)
					{
						System.out.println(ie);
					}
					if(i==1)
					{
						if(!sb1.equals("deadlink"))
						{
							ic.index(dbtime,1,sb1);
							i++;
							sb1="";
						}
					}	
					else
					{
						if(!sb1.equals("deadlink"))
						ic.index(dbtime,0,sb1);
						sb1="";
					}	

			} //end while
		
			//File f=new File("1.html");
		//	System.out.println("hello..");
			//if(f.exists())
			//	System.out.println("Nag..");
			//boolean Success=f.delete();

			//if(Success)
			//	System.out.println("File deleted..");
			//else
			//	System.out.println("Failed to delete..");
			//f.delete();
			
		    }	
			con.close();
		} //end try
		catch(ClassNotFoundException e) 
		{
			e.printStackTrace();
		}

		catch(SQLException e) 
		{
			e.printStackTrace();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}  //end main
		

	}  //end method
	public static void main(String[] args) throws Exception
	{
		ic= new IndexCreate();
		//Db_Retrieve db= new Db_Retrieve();
		Db_Retrieve.connect();
		//ic.writer.close();
		//ic.writer.optimize();
	}

}

